package IO3;

import java.io.*;

/**
 * @description: FileUtils
 * 文件操作工具类
 * 1.封装拷贝
 * 2.封装释放
 * try ..with..resource
 * @date: 2019/12/17 15:24
 * @author: Vicuna
 */


public class FileUtils {
    public static void main(String[] args) {
        InputStream is = null;
        try {
            is = new FileInputStream("src/IO3/sss.txt");
            OutputStream os = new FileOutputStream("src/IO3/sss-copy.txt");
            Copy(is, os);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    //对接输入输出流
    public static void Copy(InputStream is, OutputStream os) {
        try (is;os){    //自动释放
            //3 操作（写出）
            byte hc[] = new byte[1024];      //缓冲容器
            int len = -1;                 //接收长度；
            while ((len = is.read(hc)) != -1) {
                os.write(hc, 0, len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        finally {
//            Close(is,os);
        }

    //封装 释放
    //利用可变参数 ...
    public static void Close(Closeable... ios){
        for(Closeable io:ios){
            if (null != io) {
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
